Code for the Amazon Online Shopping System
Write the object-oriented code to implement the design of the Amazon problem.
We’ve gone over the different aspects of Amazon and observed the attributes attached to the problem using various UML diagrams. Let’s explore the more practical side of things, where we’ll implement Amazon using multiple languages. This is usually the last step in an object-oriented design interview process.
We have chosen the following languages to write the skeleton code of the different classes present in Amazon:
Java
C#
Python
C++
JavaScript
Amazon classes#
In this section, we’ll provide the skeleton code of the classes designed in the class diagram lesson.
Note: For simplicity, we are not defining getter and setter functions. The reader can assume that all class attributes are private and accessed through their respective public getter methods and modified only through their public method functions.
Constants#
The following code provides the definition of the various enums and custom data types being used in the Amazon design:
Note: JavaScript does not support enumerations so we will be using the
Object.freeze()
method as an alternative that freezes an object and prevents further modifications.
Account#
The Account
class refers to an account of a customer on Amazon and contains the personal details of a customer, such as their name, shipping address, credit card information, etc. It also provides authenticated users and admins with the functionality to add products, product reviews, and reset passwords. The definition of this class is given below:
Admin#
The Admin
class refers to a person from the administration of Amazon that can block users, and add, modify, or delete product categories. The definition of this class is provided below:
Customer#
The Customer
class is an abstract class that has the AuthenticatedUser
and Guest
classes derived from it. The classes are defined below:
Product, product category, and product review#
The following code defines the classes relating to a particular product:
Shopping cart and cart items#
The ShoppingCart
and CartItem
classes are used to add items to the shopping cart, update the quantities, and send the list of items to checkout. Both classes are defined below:
Order and order log#
The Order
class is responsible for making the payment, updating the order status, and sending the particular order to shipment. The OrderLog
class creates the log of the order and is referenced in the Order
class to keep track of all the orders. The definition of these two classes is given below:
Shipment and shipment log#
The Shipment
class keeps track of all the major details of the order’s dispatch and creates the shipment record using the ShipmentLog
class. These classes are defined below:
Payment#
The Payment
class is another abstract class with the ElectronicBankTranfer
, CreditCard
, and Cash
classes as its child classes. This takes the PaymentStatus
enum to keep track of the payment status. The definition of this class is provided below:
Notification#
The Notification
class is responsible for sending notifications to customers about the order and shipment status either via SMS or email. Since you can extend this by adding more options, the Notification
class will be an abstract class. It is defined below:
Search and catalog#
The Catalog
class contains the product information and implements the Search
interface class to enable the search functionality based on the given criteria (name and category of product). Both classes are defined below:
Wrapping up#
We’ve explored the complete design of Amazon in this chapter. We’ve looked at how Amazon is visualized using various UML diagrams and designed using object-oriented principles and design patterns.
Activity Diagram for the Amazon Online Shopping System
Getting Ready: Stack Overflow